﻿using System.Threading.Channels;

namespace In66.Net.Share.Application.Channel
{
    public class ChannelAccessor<TModel>
    {
        private static readonly Lazy<ChannelAccessor<TModel>> lazy = new(() => new ChannelAccessor<TModel>());

        static ChannelAccessor()
        {
        }

        private ChannelAccessor()
        {
            var channelOptions = new BoundedChannelOptions(1000)
            {
                FullMode = BoundedChannelFullMode.DropOldest
            };
            var channel = System.Threading.Channels.Channel.CreateBounded<TModel>(channelOptions);
            Writer = channel.Writer;
            Reader = channel.Reader;
        }

        public static ChannelAccessor<TModel> Instance => lazy.Value;

        public ChannelWriter<TModel> Writer { get; }

        public ChannelReader<TModel> Reader { get; }
    }
}
